package com.xs.project.service.impl;


import com.xs.project.common.Result;
import com.xs.project.mapper.ClassManagerMapper;
import com.xs.project.pojo.vo.StudentLeaveClassReq;
import com.xs.project.pojo.vo.UserInfoAndCourseReq;
import com.xs.project.pojo.vo.resp.StudentHomeWork;
import com.xs.project.pojo.vo.resp.StudentLeaveClassResp;
import com.xs.project.pojo.vo.resp.UserInfoAndCourse;
import com.xs.project.service.IClassManagerService;
import com.xs.project.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClassManagerServiceImpl implements IClassManagerService {
    @Resource
    private ClassManagerMapper classManagerMapper;
    @Override
    public Result<List<StudentLeaveClassResp>> selectLeaveClass(StudentLeaveClassReq request) {
        List<StudentLeaveClassResp> list = classManagerMapper.selectLeaveClass(request);
        if (CollectionUtils.isEmpty(list)){
            return Result.success(new ArrayList<>());
        }
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<StudentLeaveClassResp> pageRes = list.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        for (StudentLeaveClassResp re : pageRes) {
            if ("0".equals(re.getStatus())){
                re.setStatusStr("未处理");
            }else if ("1".equals(re.getStatus())){
                re.setStatusStr("已同意");
            }else{
                re.setStatusStr("已拒绝");
            }
            re.setCreateTimeStr(DateUtil.getDateStr(re.getCreateTime()));
        }
        return Result.success(pageRes,list.size());
    }

    @Override
    public Result<List<UserInfoAndCourse>> selectUserInfoAndCourse(UserInfoAndCourseReq request) {
        List<UserInfoAndCourse> list = classManagerMapper.selectUserInfoAndCourse(request);
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<UserInfoAndCourse> pageRes = list.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        return Result.success(pageRes,list.size());
    }

    @Override
    public Boolean handleLeaveClass(Integer id, String status) {
        classManagerMapper.handleLeaveClass(id,status);
        return true;
    }
}
